#ifndef _KC_STARTTLSRECEIVER_H__KC_
#define _KC_STARTTLSRECEIVER_H__KC_

#include "Receiver.h"
#include "SSLReceiver.h"
#include "../../logs/Logger.h"

namespace kc_sf
{
	/** Receiver, który rozpoczyna rozmawę z klientem na niezabezpieczonym kanale
	 * ale szyfruje sesję po komendzie STARTTLS i udanym uścisku dłoni */
	class StartTLSReceiver : public CommRequestHandler
	{
		public:

			StartTLSReceiver() {}
			explicit StartTLSReceiver( const Connection &connection );
			~StartTLSReceiver();

			CommRequestHandler *getInstance( const Connection &connection ) const;
			void operator()();

		private:

			int r_client_socket;
			Receiver *r_unsafe_receiver;
			SSLReceiver *r_safe_receiver;
			SSLServerSession *r_safe_session;
			Logger r_logger;
			ReceiverState r_state;

			CommResult processHelo( const Command & cmd );
			CommResult processEhlo( const Command & cmd );
			CommResult processNoop();
			CommResult processQuit();
			CommResult processRset();
			CommResult processUnimplemented( const Command & cmd );
			CommResult processUndefined( const Command & cmd );

			CommResult processAuthPlain();
			CommResult processAuthLogin();
			CommResult processStartTLS();
			CommResult processMailFrom(const Command & cmd);
			CommResult processRcpt(const Command & cmd);
			CommResult processData();

			CommResult recvCommand( Command & cmd, int timeout_s = DEF_RECEIVER_TIMEOUT_S );
			CommResult recvLine( std::string &to, int timeout_s = DEF_RECEIVER_TIMEOUT_S );
			CommResult sendReply( const Reply & reply, int timeout_s = DEF_RECEIVER_TIMEOUT_S);

			void clear();
	};
}

#endif // _KC_STARTTLSRECEIVER_H__KC_
